<template>
  <div class="container">
    <div class="app-container">

      <div class="role-operate">
        <el-button size="mini" type="primary" @click="centerDialogVisible">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template v-slot="{ row }">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
          </template></el-table-column>
        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" type="textarea" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="btnEditOK(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="btnEditRow(row)">编辑</el-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增弹窗 -->
      <el-dialog
        title="新增角色"
        :visible.sync="dialogVisible"
        width="500px"
        @close="close"
      >
        <el-form ref="addrole" label-width="120px" :model="formData" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item label="启用" prop="state">
            <el-switch
              v-model="formData.state"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" style="width:300px " type="textarea" size="mini" />
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="12" :offset="6">
                <el-button type="primary" size="mini" @click="addRoleList">确 定</el-button>
                <el-button size="mini" @click="close">取 消</el-button>
              </el-col>
            </el-row>

          </el-form-item>
        </el-form>

      </el-dialog>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination layout="prev, pager, next" :total="total" :current-page="params.page" :page-size="params.pagesize" @current-change="handleCurrentChange" />
      </el-row>
    </div>
  </div>
</template>
<script>
import { addRoleList, getRoleList, updateRole, delRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      dialogVisible: false,
      formData: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows, total } = await getRoleList(this.params)
      this.list = rows
      this.total = total
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getRoleList()
    },
    centerDialogVisible() {
      this.dialogVisible = true
    },
    addRoleList() {
      this.$refs.addrole.validate(async valid => {
        if (!valid) return
        console.log(this.formData)
        await addRoleList(this.formData)
        this.getRoleList()
        this.close()
      })
    },
    btnEditRow(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    async btnEditOK(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id })
        this.$message.success('更新角色成功')
        // eslint 校验误判  两种办法解决
        // 第一种工具快速修复
        // // eslint-disable-next-line require-atomic-updates
        // row.name = row.editRow.name
        // 第二种 浅拷贝
        Object.assign(row, {
          ...row.editRow,
          isEdit: false // 退出编辑模式
        }) // 规避eslint的误判
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    async confirmDel(id) {
      await delRole(id)
      this.$message.success('删除角色成功')
      if (this.list.length === 1) this.params.page--
      this.getRoleList()
    },
    close() {
      this.dialogVisible = false
      this.$refs.addrole.resetFields()
    }
  }
}
</script>
<style scoped lang="scss">
.role-operate {
  padding: 20px 20px;
}
::v-deep .el-textarea__inner{
  line-height:2 !important;
}
</style>
